﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Miner_Highlearn;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Highlearner_API
{
	/// <summary>
	/// A testing class. Used for speeding up debugging
	/// </summary>
	public class TestingUtilites
	{
		public static void SaveHLItems(List<HLItem> a_oList, String a_sFilename)
		{
			Hashtable l_oMainHashtable = new Hashtable();
			Hashtable l_oItem;

			foreach (HLItem l_oHLItem in a_oList)
			{
				l_oItem = new Hashtable();
				l_oItem["IID"] = l_oHLItem.IID;
				l_oItem["Name"] = l_oHLItem.Name;
				l_oItem["Path"] = l_oHLItem.Path;
				l_oItem["SentFrom"] = l_oHLItem.SentFrom;
				l_oItem["SID"] = l_oHLItem.SID;

				l_oMainHashtable[l_oHLItem.IID] = l_oItem;
			}

			FileStream l_oFilestream = File.Open(a_sFilename, FileMode.Create);
			BinaryFormatter b = new BinaryFormatter();
			b.Serialize(l_oFilestream, l_oMainHashtable);
			l_oFilestream.Close();
		}

		public static void SimulateCourseLoading(Executer a_oExecuter, String a_sCourseName, String a_sFilename)
		{
			a_oExecuter.DataCenter.Courses.Add(a_sCourseName, new Course(a_sCourseName, 14));

			LoadHLItems(a_oExecuter.DataCenter, a_sFilename, a_sCourseName);

			a_oExecuter.PerformOnCoursesLoadedEvent();
		}

		public static void LoadHLItems(CDataCenter a_oDC, String a_sFilename, String a_sCourseName)
		{
			Hashtable l_oMaintable = null;
			List<HLItem> l_oList = new List<HLItem>();
			
			Hashtable l_oHashItem;
			HLItem l_oItem;

			// Open the file containing the data that you want to deserialize.
			FileStream l_oFileStream = new FileStream(a_sFilename, FileMode.Open);
			try
			{
				BinaryFormatter b = new BinaryFormatter();
				l_oMaintable = (Hashtable)b.Deserialize(l_oFileStream);

				foreach (String Key in l_oMaintable.Keys)
				{
					l_oItem = new HLItem();

					l_oHashItem = (Hashtable)l_oMaintable[Key];
					l_oItem.IID = (string)l_oHashItem["IID"];
					l_oItem.Name = (string)l_oHashItem["Name"];
					l_oItem.Path = (string)l_oHashItem["Path"];
					l_oItem.SentFrom = (string)l_oHashItem["SentFrom"];
					l_oItem.SID = (string)l_oHashItem["SID"];

					l_oList.Add(l_oItem);
				}				
			}
			finally
			{
				l_oFileStream.Close();
			}

			a_oDC.ParseFolders(a_sCourseName, l_oList);
		}

		public static void CreateDummyCourse(CDataCenter a_oDataCenter)
		{
			String l_sCourseName = "דוגמא";
			HLItem l_oItem;
			List<HLItem> l_oList = new List<Miner_Highlearn.HLItem>();
			
			a_oDataCenter.Courses.Add(l_sCourseName, new Course(l_sCourseName, 14));

			l_oItem = new HLItem();
			l_oItem.Name = "קובץ1";
			l_oItem.Path = "ספריה1";
			l_oList.Add(new HLItem());

			l_oItem = new HLItem();
			l_oItem.Name = "קובץ2";
			l_oItem.Path = "ספריה1";
			l_oList.Add(new HLItem());

			l_oItem = new HLItem();
			l_oItem.Name = "קובץ1";
			l_oItem.Path = "ספריה2";
			l_oList.Add(new HLItem());

			l_oItem = new HLItem();
			l_oItem.Name = "קובץ2";
			l_oItem.Path = "ספריה2";
			l_oList.Add(new HLItem());

			l_oItem = new HLItem();
			l_oItem.Name = "קוב31";
			l_oItem.Path = "ספריה1";
			l_oList.Add(new HLItem());

			a_oDataCenter.ParseFolders(l_sCourseName, l_oList);

		}

		public static void SaveException(Exception e, String a_sFilename)
		{
			Hashtable l_oItem;

			l_oItem = new Hashtable();
			l_oItem["Message"] = e.Message;
			l_oItem["Trace"] = e.StackTrace;
			l_oItem["Data"] = e.Data;

			FileStream l_oFilestream = File.Open(a_sFilename, FileMode.Create);
			BinaryFormatter b = new BinaryFormatter();
			b.Serialize(l_oFilestream, l_oItem);
			l_oFilestream.Close();
		}
	}
}
